package com.dinian.demeter.controller;

import com.dinian.demeter.common.domain.ResponseBo;
import com.dinian.demeter.domain.Fault;
import com.dinian.demeter.service.FaultService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@Controller
public class FaultController {

    @Autowired
    private FaultService faultService;

    /**
     * 测试
     * @return
     */
    @GetMapping("/userTable")
    public String userTable(){
        return "user/userTable";
    }

    /**
     * 添加故障信息
     * @param fault
     * @return
     */
    @ResponseBody
    @RequestMapping("/addFault")
    public ResponseBo addFault(Fault fault){
        Boolean isAdd = faultService.addFault(fault);
        log.info("add fault is :{}",isAdd);
        if (isAdd)
            return ResponseBo.ok();
        return ResponseBo.error();
    }

    /**
     * 根据ids删除故障信息
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping("/deleteFault")
    public ResponseBo removeFault(String ids){
        Boolean isDelete = faultService.removeFault(ids);
        log.info("delete fault is :{}",isDelete);
        if (isDelete)
            return ResponseBo.ok("删除成功");
        return ResponseBo.error();
    }

    /**
     * 修改故障信息
     * @param fault
     * @return
     */
    @ResponseBody
    @RequestMapping("/modifyFault")
    public ResponseBo modifyFault(Fault fault){
        log.info("fault is :{}",fault);
        Boolean isModify = faultService.modifyFault(fault);
        log.info("modify fault is :{}",isModify);
        if (isModify)
            return ResponseBo.ok();
        return ResponseBo.error();
    }

    /**
     * 查询故障信息ByUser
     * @param userId
     * @return
     */
    @ResponseBody
    @RequestMapping("/queryFaultByUser")
    public List<Fault> queryFaultByUser(String userId){
        List<Fault> faultList = faultService.findFaultByUserId(userId);
        log.info("query fault num :{}",faultList.size());
        return faultList;
    }

    /**
     * 根据faultId查询故障信息
     * @param faultId
     * @return
     */
    @ResponseBody
    @RequestMapping("/queryFaultById")
    public ResponseBo queryFaultById(String faultId){
        Fault fault = faultService.findFaultById(faultId);
        log.info("query fault by faultId success");
        return ResponseBo.ok(fault);
    }
}
